<template>
  <div class="my-body">
    <div class="container">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input
              auto-complete="on"
              prefix-icon="el-icon-user"
              v-model="form.username"
              placeholder="请输入账户"></el-input>
        </el-form-item>
        <el-form-item label="密  码">
          <el-input
              auto-complete="on"
              show-password
              prefix-icon="el-icon-unlock"
              v-model="form.password"
              @keydown.enter.native="login"
              placeholder="请输入密码"
              ></el-input>
        </el-form-item>
        <span style="color: red">{{msg}}</span>
        <div>
          <el-button class="button register" v-if="can_register"
              @click="$router.push('/register')">注册</el-button>
          <el-button class="button login"
                     @click="login">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import userinfo from "@/store/userinfo/userinfo";
import {reqCanRegister} from "@/api/user";
import {requestGetData, requestIsSuccess} from "@/api/request";

export default {
  name: "Login",
  data(){
    return{
      form:{
        username:'',
        password:''
      },
      msg:'',
      can_register:false
    }
  },
  methods:{
    login() {
      this.$store.dispatch("userLogin", {...this.form}).then(loginResult => {
        if (loginResult === true) {
          this.$store.dispatch("getUserInfo").then(getInfoResult => {
            if(getInfoResult=='ok'){
              //获取到用户信息
              const nextRouter =  localStorage.getItem("nextRoute")
              if(nextRouter!=null&&nextRouter!="Login"&&nextRouter!="Register"){
                localStorage.removeItem("nextRoute")
                this.$router.push({
                  name:nextRouter
                })
              }else{
                this.$router.push('/')
              }
            }
          });
        }
      });
    }
  },
  mounted() {
    reqCanRegister().then(result=>{
      if(requestIsSuccess(result)){
        this.can_register=requestGetData(result)
      }
    })
  }
}
</script>

<style scoped>
.my-body{
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
  height: 100vh;
  background-image: url("/public/login-back.jpeg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
.container {
  max-width: 400px;
  margin: 100px auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  position: absolute;
  top: 40%;       /* 居中 */
  right: 35%;       /* 右边 */
  transform: translate(0, -50%)
}
h1 {
  text-align: center;
}
input[type="password"] {
  width: 100%;
  padding: 10px;
  margin-bottom: 20px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}
input[type="submit"] {
  width: 100%;
  padding: 10px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}
input[type="submit"]:hover {
  background-color: #0056b3;
}
.login{
  float: right;
  right: 10px;
  background-color: #5f99c7;
}
.register{
  border: none;
  text-decoration: underline;
}
</style>